package com.xrj.gateway.controller;

import com.mall.common.annotation.PassToken;
import com.mall.common.response.Response;
import com.mall.common.validate.ValidationUtils;
import com.mall.promotion.constant.PromotionServiceEnum;
import com.mall.promotion.dto.bo.resp.SecSkillListResp;
import com.mall.promotion.dto.vo.SecKillListVo;
import com.mall.promotion.service.TbPromoItemService;
import com.mall.promotion.service.TbPromoSessionService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author: xrj
 * @date: 2022/5/2 22:04
 * @description:
 */
@RestController
@Slf4j
@RequestMapping("/shopping")
public class PromotionController {

    @Reference(timeout = 3000,check = false)
    private TbPromoSessionService sessionService;

    @Reference(timeout = 3000,check = false)
    private TbPromoItemService itemService;

    @ApiOperation(value = "秒杀场次信息",httpMethod = "GET")
    @GetMapping("/seckilllist")
    @PassToken
    public Object getSecKillList(SecKillListVo secKillListVo){
        ValidationUtils.ValidResult validResult = ValidationUtils.validateBean(secKillListVo);
        if (validResult.isHasErrors()){
            return Response.checkFailed(validResult.getErrorsStr());
        }
        //处理当前时间参数
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        secKillListVo.setDate(date);
        SecSkillListResp resp = sessionService.getSecSkillInfo(secKillListVo);
        if (!PromotionServiceEnum.SUCCESS.getCode().equals(resp.getCode())){
            return Response.operationFailed(resp.getMsg());
        }
        return Response.success(resp.getSecSkillListModel(),"秒杀信息查询成功!");
    }
}
